package com.yonyou.cyxdms.basedata.service.service.feign;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.basedata.service.dto.dmsFeign.SaveOrUpdateColorOrTrimDTO;
import com.yonyou.cyxdms.basedata.service.entity.color.ColorPO;
import com.yonyou.cyxdms.basedata.service.dto.dmsFeign.SaveOrUpdateProductDTO;
import com.yonyou.cyxdms.basedata.service.entity.color.ColorPO;
import com.yonyou.cyxdms.basedata.service.entity.product.VsProductPO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.basedata.service.constant.BasedataDictConstants;
import com.yonyou.cyxdms.basedata.service.dto.dmsFeign.ResponseDTO;
import com.yonyou.cyxdms.basedata.service.dto.dmsFeign.SaveOrUpdateBasicAllDTO;
import com.yonyou.cyxdms.basedata.service.entity.brand.BrandPO;
import com.yonyou.cyxdms.basedata.service.entity.configurer.PackagePO;
import com.yonyou.cyxdms.basedata.service.entity.model.ModelPO;
import com.yonyou.cyxdms.basedata.service.entity.option.VsOptionPO;
import com.yonyou.cyxdms.basedata.service.entity.series.SeriesPO;
import com.yonyou.cyxdms.basedata.service.feign.DmsVehicleClient;
import com.yonyou.cyxdms.basedata.service.repository.brand.BrandMapper;
import com.yonyou.cyxdms.basedata.service.repository.color.ColorMapper;
import com.yonyou.cyxdms.basedata.service.repository.configurer.PackageMapper;
import com.yonyou.cyxdms.basedata.service.repository.model.ModelMapper;
import com.yonyou.cyxdms.basedata.service.repository.option.VsOptionMapper;
import com.yonyou.cyxdms.basedata.service.repository.product.VsProductMapper;
import com.yonyou.cyxdms.basedata.service.repository.series.SeriesMapper;


@Service
public class VehicleIntoToDmsServiceImpl implements VehicleIntoToDmsService {
	private static final Logger logger = LoggerFactory.getLogger(VehicleIntoToDmsServiceImpl.class);

	@Autowired
	private DmsVehicleClient dmsVehicleClient;
	
	@Autowired
    private BrandMapper brandMapper;

    @Autowired
    private SeriesMapper seriesMapper;

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private PackageMapper packageMapper;
    
    @Autowired
    private VsOptionMapper vsOptionMapper;
    
    @Autowired
    private ColorMapper colorMapper;
    
    @Autowired
    private VsProductMapper vsProductMapper;

	//定时任务下发车辆配置信息
	@Override
	public void sendVehicleAllInfoToDms(Map<String, Object> queryParam) {
		// 先查询出需要下发的数据:IS_ISSUE为未下发或者下发失败，查询结果格式为List<SaveOrUpdateBasicAllDTO>；
		List<SaveOrUpdateBasicAllDTO> list=brandMapper.selectVechileAllInfo(queryParam);
		//查询数据判断非空
		if(!CommonUtils.isNullOrEmpty(list)&&list.size()>0){
	        logger.info("========车辆主数据下发数据{}", JSON.toJSON(list));

			//不为空，调用对应的feign接口
			ResponseDTO result=dmsVehicleClient.sendVehicleAllInfoToDms(list);
			logger.info("========车辆主数据下发回调信息{}", JSON.toJSON(result));
			LocalDateTime sendTime=LocalDateTime.now();
			if("0".equals(result.getReturnCode())){
				//下发成功，更新对应业务表：IS_ISSUE为下发成功、SEND_DATE为当前时间、SEND_BY默认"-1"
				list.forEach(t->{
					if(!StringUtils.isNullOrEmpty(t.getBrandId())) {
						BrandPO brandPo=brandMapper.selectById(t.getBrandId());
						brandPo.setIsIssue(BasedataDictConstants.IS_SEND_SUCCESS);
						brandPo.setSendDate(sendTime);
						brandPo.setSendBy("-1");
						brandMapper.updateById(brandPo);
					}
					
					if(!StringUtils.isNullOrEmpty(t.getSeriesId())) {
						SeriesPO seriesPo=seriesMapper.selectById(t.getSeriesId());
						seriesPo.setIsIssue(BasedataDictConstants.IS_SEND_SUCCESS);
						seriesPo.setSendDate(sendTime);
						seriesPo.setSendBy("-1");
						seriesMapper.updateById(seriesPo);
					}
					
					if(!StringUtils.isNullOrEmpty(t.getModelId())) {
						ModelPO modelPo=modelMapper.selectById(t.getModelId());
						modelPo.setIsIssue(BasedataDictConstants.IS_SEND_SUCCESS);
						modelPo.setSendDate(sendTime);
						modelPo.setSendBy("-1");
						modelMapper.updateById(modelPo);
					}
					
					if(!StringUtils.isNullOrEmpty(t.getConfigId())){
						PackagePO packagePo=packageMapper.selectById(t.getConfigId());
						packagePo.setIsIssue(BasedataDictConstants.IS_SEND_SUCCESS);
						packagePo.setSendDate(sendTime);
						packagePo.setSendBy("-1");
						packageMapper.updateById(packagePo);
					}
					
					
					if(t.getOptionId()!=null&&t.getOptionId()!=0){
						VsOptionPO vsOptionPo=vsOptionMapper.selectById(t.getOptionId());
						vsOptionPo.setIsIssue(BasedataDictConstants.IS_SEND_SUCCESS);
						vsOptionPo.setSendDate(sendTime);
						vsOptionPo.setSendBy("-1");
						vsOptionMapper.updateById(vsOptionPo);
					}
					
				});
			}else {
				//下发失败，更新对应业务表：IS_ISSUE为下发失败、下发时间为当前时间、下发人默认"-1"
				list.forEach(t->{
					if(!StringUtils.isNullOrEmpty(t.getBrandId())) {
						BrandPO brandPo=brandMapper.selectById(t.getBrandId());
						brandPo.setIsIssue(BasedataDictConstants.IS_SEND_FAILTURE);
						brandPo.setSendDate(sendTime);
						brandPo.setSendBy("-1");
						brandMapper.updateById(brandPo);
					}
					
					if(!StringUtils.isNullOrEmpty(t.getSeriesId())) {
						SeriesPO seriesPo=seriesMapper.selectById(t.getSeriesId());
						seriesPo.setIsIssue(BasedataDictConstants.IS_SEND_FAILTURE);
						seriesPo.setSendDate(sendTime);
						seriesPo.setSendBy("-1");
						seriesMapper.updateById(seriesPo);
					}
					
					if(!StringUtils.isNullOrEmpty(t.getModelId())) {
						ModelPO modelPo=modelMapper.selectById(t.getModelId());
						modelPo.setIsIssue(BasedataDictConstants.IS_SEND_FAILTURE);
						modelPo.setSendDate(sendTime);
						modelPo.setSendBy("-1");
						modelMapper.updateById(modelPo);
					}
					
					if(!StringUtils.isNullOrEmpty(t.getConfigId())) {
						PackagePO packagePo=packageMapper.selectById(t.getConfigId());
						packagePo.setIsIssue(BasedataDictConstants.IS_SEND_FAILTURE);
						packagePo.setSendDate(sendTime);
						packagePo.setSendBy("-1");
						packageMapper.updateById(packagePo);
					}
					
					if(t.getOptionId()!=null&&t.getOptionId()!=0){
						VsOptionPO vsOptionPo=vsOptionMapper.selectById(t.getOptionId());
						vsOptionPo.setIsIssue(BasedataDictConstants.IS_SEND_FAILTURE);
						vsOptionPo.setSendDate(sendTime);
						vsOptionPo.setSendBy("-1");
						vsOptionMapper.updateById(vsOptionPo);
					}
				});
			}
			logger.info("========车辆主数据下发回调信息{}", JSON.toJSON(result));
		}
	}
	
	//定时任务下发车辆颜色信息
	@Override
	public void sendVehicleColorInfoToDms(Map<String, String> queryParam) {
		// TODO Auto-generated method stub
		// 先查询出需要下发的数据:IS_ISSUE为未下发或者下发失败，查询结果格式为List<SaveOrUpdateBasicAllDTO>；
		List<SaveOrUpdateColorOrTrimDTO> list=colorMapper.sendVehicleColorInfo(queryParam);
		//查询数据判断非空
		if(!CommonUtils.isNullOrEmpty(list)&&list.size()>0){
			//不为空，调用对应的feign接口
			ResponseDTO result=dmsVehicleClient.sendVehicleColorInfoToDms(list);
			logger.info("========车辆颜色下发回调信息{}", JSON.toJSON(result));
			LocalDateTime sendTime=LocalDateTime.now();
			if("0".equals(result.getReturnCode())){
				//下发成功，更新对应业务表：IS_ISSUE为下发成功、SEND_DATE为当前时间、SEND_BY默认"-1"
				list.forEach(t->{
                        if (!StringUtils.isNullOrEmpty(t.getColorId())){
//                        	if (!StringUtils.isNullOrEmpty(t.getColorId())) {
								ColorPO colorPo = colorMapper.selectById(t.getColorId());
								colorPo.setIsIssue(BasedataDictConstants.IS_SEND_SUCCESS);
								colorPo.setSendDate(sendTime);
								colorPo.setSendBy("-1");
								colorMapper.updateById(colorPo);
							}
//                        }

				});
			}else {
				//下发失败，更新对应业务表：IS_ISSUE为下发失败、下发时间为当前时间、下发人默认"-1"
				list.forEach(t->{
//					 if (!StringUtils.isNullOrEmpty(t.getColorId())) {
						 ColorPO colorPo = colorMapper.selectById(t.getColorId());
						 colorPo.setIsIssue(BasedataDictConstants.IS_SEND_FAILTURE);
						 colorPo.setSendDate(sendTime);
						 colorPo.setSendBy("-1");
						 colorMapper.updateById(colorPo);
//					 }

				});
			}

		}
	}

	//定时任务下发车辆主数据信息
	@Override
	public void sendVehicleProductInfoToDms(Map<String, String> queryParam) {
		// 先查询出需要下发的数据:IS_ISSUE为未下发或者下发失败，查询结果格式为List<SaveOrUpdateBasicAllDTO>；
		List<SaveOrUpdateProductDTO> list = vsProductMapper.selectVehicleProductInfo(queryParam);
		//查询数据判断非空
		if(!CommonUtils.isNullOrEmpty(list) && list.size() > 0){
			logger.info("=======产品主数据下发数据{}",JSON.toJSON(list));
			//不为空，调用对应的feign接口
			ResponseDTO result = dmsVehicleClient.sendVehicleProductInfoToDms(list);
			logger.info("========产品主数据下发回调信息{}", JSON.toJSON(result));
			LocalDateTime sendTime = LocalDateTime.now();
			if("0".equals(result.getReturnCode())){
				//下发成功，更新对应业务表：IS_ISSUE为下发成功、SEND_DATE为当前时间、SEND_BY默认"-1"
				list.forEach(t->{
					//产品
					if (!StringUtils.isNullOrEmpty(t.getProductId())){
						VsProductPO vsProductPo = vsProductMapper.selectById(t.getProductId());
						vsProductPo.setIsIssue(BasedataDictConstants.IS_SEND_SUCCESS);
						vsProductPo.setSendDate(sendTime);
						vsProductPo.setSendBy("-1");
						vsProductMapper.updateById(vsProductPo);
					}
				});
			}else {
				//下发失败，更新对应业务表：IS_ISSUE为下发失败、下发时间为当前时间、下发人默认"-1"
				list.forEach(t->{
					//产品
					if (!StringUtils.isNullOrEmpty(t.getProductId())){
						VsProductPO vsProductPo = vsProductMapper.selectById(t.getProductId());
						vsProductPo.setIsIssue(BasedataDictConstants.IS_SEND_FAILTURE);
						vsProductPo.setSendDate(sendTime);
						vsProductPo.setSendBy("-1");
						vsProductMapper.updateById(vsProductPo);
					}
				});
			}
		}
	}


}
